<?php
declare(strict_types=1);

namespace plugin\ky_black\app\admin\model;

use support\Model;

class Organization extends Model
{
    protected $table = 'ky_organization';
    protected $primaryKey = 'org_id';
    
    // 添加默认值属性
    protected $attributes = [
        'status' => 1  // 默认状态为正常
    ];

    // 添加属性转换
    protected $casts = [
        'black_card_no' => 'string'
    ];
    
    // 修改获取器，处理空字符串
    public function getBlackCardNoAttribute($value)
    {
        return $value === '' ? null : $value;
    }
    
    // 修改修改器，处理空字符串
    public function setBlackCardNoAttribute($value)
    {
        $this->attributes['black_card_no'] = $value === '' ? null : $value;
    }
    
    protected $fillable = [
        'company_name',
        'business_license',
        'legal_representative',
        'company_address',
        'contact_phone',
        'company_email',
        'invoice_type',
        'tax_number',
        'bank_name',
        'bank_account',
        'invoice_address',
        'invoice_phone',
        'contact_name',
        'contact_mobile',
        'status',
        'black_card_no'
    ];

    /**
     * 关联变更记录
     */
    public function changes()
    {
        return $this->hasMany(OrganizationChangeLog::class, 'org_id', 'org_id');
    }

    /**
     * 关联黑卡
     */
    public function blackCards()
    {
        return $this->hasMany(BlackCard::class, 'org_id', 'org_id');
    }
} 